﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ZDSoft.UPMS.Manager;
using ZDSoft.UPMS.Model;
using ZDSoft.UPMS.Service;
using NHibernate.Criterion;
using System.Data.SqlClient;

namespace ZDSoft.UPMS.Component
{
    public class ComponentBase<T, U> : IServiceBase<T>
        where T : ModelBase<T>, new()//限定T的类型为EntityBase
        where U : ManagerBase<T>, new()//限定U的类型为ManagerBase
    {
        //用反射实例化U
        U u = (U)typeof(U).GetConstructor(Type.EmptyTypes).Invoke(null);

        /// <summary>
        /// 创建一个实体
        /// </summary>
        /// <param name="t">实体</param>
        public void Create(T t)
        {
            u.Create(t);
        }

        /// <summary>
        /// 修改一个实体
        /// </summary>
        /// <param name="t">实体</param>
        public void Update(T t)
        {
            u.Update(t);
        }

        /// <summary>
        /// 删除一个实体
        /// </summary>
        /// <param name="id">实体ID</param>
        public void Delete(int id)
        {
            u.Delete(id);
        }

        /// <summary>
        /// 查找一个实体
        /// </summary>
        /// <param name="id">实体ID</param>
        /// <returns>实体</returns>
        public T FindByID(int id)
        {
            return u.FindByID(id);
        }

        /// <summary>
        /// 查找所有实体
        /// </summary>
        /// <returns>实体集合</returns>
        public IList<T> FindAll()
        {
            return u.FindAll();
        }

        /// <summary>
        /// 根据查询条件获取所有满足条件的实体
        /// </summary>
        /// <param name="queryConditions">查询条件，可以是多个,形式如："UserName"="Lion"</param>
        /// <returns>满足条件的实体集合</returns>
        public IList<T> Find(IList<ICriterion> queryConditions)
        {
            return u.Find(queryConditions);
        }

        /// <summary>
        /// 分页获取对象集合
        /// </summary>
        /// <param name="queryConditions">查询条件，可以是多个,形式如："UserName"="Michael"</param>
        /// <param name="orderList">排序方式，根据字段排序，形式如："UserName","DESC"</param>
        /// <param name="pageIndex">当前页码</param>
        /// <param name="pageSize">页面大小</param>
        /// <param name="count">满足条件的记录总数</param>
        /// <returns>返回满足条件的实体集合</returns>
        public IList<T> GetPaged(IList<ICriterion> queryConditions, IList<Order> orderList, int pageIndex,
            int pageSize, out int count)
        {
            return u.GetPaged(queryConditions, orderList, pageIndex, pageSize, out count);
        }


        /// <summary>
        /// 根据查询条件分页获取实体
        /// </summary>
        /// <param name="queryConditions">查询条件集合</param>
        /// <returns>返回满足查询条件的实体</returns>
        public IList<T> Find(IList<QueryConditions> queryConditions)
        {
            return u.Find(queryConditions);
        }

        /// <summary>
        /// 执行Sql语句
        /// </summary>
        /// <param name="str"></param>
        public void Exec(string str)
        {
            u.Exec(str);
        }
        public bool IsHaveFunction(string Name)
        {
            return u.IsHaveFunction(Name);
        }

        public void ExeNonQuery(string procedure, SqlParameter[] parameter)
        {
            u.ExeNonQuery(procedure, parameter);
        }
        public SqlParameter[] CreateSqlParameter(List<string> SqlParameterNames, List<Object> SqlValues)
        {
            return u.CreateSqlParameter(SqlParameterNames, SqlValues);
        }
    }
}
